package net.huashitong.scheduling.web;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.huashitong.Indicators.filter.IndicatorBreakdownFilter;
import net.huashitong.Indicators.service.IndicatorBreakdownService;
import net.huashitong.scheduling.databean.IndicatorSchedulingDatabean;
import net.huashitong.scheduling.entity.AreaNumber;
import net.huashitong.scheduling.entity.IndicatorScheduling;
import net.huashitong.scheduling.filter.IndicatorSchedulingFilter;
import net.huashitong.scheduling.service.AreaNumberService;
import net.huashitong.scheduling.service.IndicatorSchedulingService;
import net.huashitong.scheduling.utils.AreaNameUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.Message;

@Controller
@RequestMapping("/app/indicatorScheduling/")
public class IndicatorSchedulingController extends JsonBaseController {

	@Autowired
	private IndicatorSchedulingService schedulingService;
	@Autowired
	private IndicatorBreakdownService indicatorBreakdownService;
	@Autowired
	private AreaNumberService areaNumberService;
	
	@Override
	protected String getView(String arg0) {
		// TODO Auto-generated method stub
		return null;
	}
	/**
	 * 处理调度数据
	 * @param filter
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/detailIndicator")
	public Message detailIndicator(@RequestBody IndicatorSchedulingFilter filter){
		List<IndicatorScheduling> indicatorSchedulings = filter.getLists();
		filter.setMonth("09");
		List<IndicatorScheduling> lists = schedulingService.queryByCategory(filter);
		for (IndicatorScheduling indicatorScheduling : indicatorSchedulings) {
			for (IndicatorScheduling indicatorSchedul : lists) {
				if(indicatorScheduling.getIndicatorName().equals(indicatorSchedul.getIndicatorName())){
					Double doubles = Double.parseDouble(indicatorScheduling.getRatio());
					String st = "";
					if(doubles != 0){
						DecimalFormat df=new DecimalFormat(".##");
						st=df.format(doubles);
						indicatorSchedul.setRatio(st);
					}else {
						indicatorSchedul.setRatio(indicatorScheduling.getRatio());
						st = indicatorSchedul.getRatio();
					}
					Double double1 = Double.parseDouble(st);
					Double double2 = Double.parseDouble(indicatorSchedul.getLastYearValue());
					if("0".equals(indicatorSchedul.getRatio())){
						indicatorSchedul.setYearPredict("0");
					}else {
						indicatorSchedul.setYearPredict(Double.toString(double2*(100+double1)/100));
					}
				}
			}
		}
		if("0".equals(lists.get(0).getRatio())){
			Double double3 = new Double(0);
			for (int j = 0; j < lists.size(); j++) {
				if(j == 0){
					continue;
				}
				double3 += Double.parseDouble(lists.get(j).getYearPredict());
			}
			
			lists.get(0).setYearPredict(Double.toString(double3));
			Double double4 = Double.parseDouble(lists.get(0).getLastYearValue());
			DecimalFormat df=new DecimalFormat(".##");
			String st=df.format((double3-double4)/double4*100);
			lists.get(0).setRatio(st);
		}else {
			Double double3 = Double.parseDouble(lists.get(0).getYearPredict());
			Double double4 = new Double(0);
			int k = 0;
			for (int i = 0; i < lists.size(); i++) {
				if(i == 0){
					continue;
				}else if("0".equals(lists.get(i).getRatio())){
					k = i;
				}else {
					double4 += Double.parseDouble(lists.get(i).getYearPredict());
				}
			}
			lists.get(k).setYearPredict(Double.toString(double3-double4));
			Double double5 = Double.parseDouble(lists.get(k).getLastYearValue());
			DecimalFormat df=new DecimalFormat(".##");
			String st=df.format(((double3-double4-double5)/double5*100));
			lists.get(k).setRatio(st);
		}
		List<IndicatorSchedulingDatabean> databeans = new ArrayList<IndicatorSchedulingDatabean>();
		for (IndicatorScheduling indicatorScheduling : lists) {
			IndicatorSchedulingDatabean databean = new IndicatorSchedulingDatabean();
			databean.setIndicatorName(indicatorScheduling.getIndicatorName());
			databean.setRatio(indicatorScheduling.getRatio());
			databeans.add(databean);
		}
		return coverMessage("200", "获取成功", databeans);
	}
	/**
	 * 查询指标数据
	 * @param filter
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/queryValue")
	public Message queryValue(@RequestBody IndicatorSchedulingFilter filter){
		if("GDP".equals(filter.getIndicatorsCategory())){
			filter.setMonth("09");
		}
		
		List<IndicatorScheduling> lists = schedulingService.queryByCategory(filter);
		List<IndicatorSchedulingDatabean> databeans = new ArrayList<IndicatorSchedulingDatabean>();
		for (IndicatorScheduling indicatorScheduling : lists) {
			IndicatorSchedulingDatabean databean = new IndicatorSchedulingDatabean();
			databean.setIndicatorName(indicatorScheduling.getIndicatorName());
			databean.setRatio(indicatorScheduling.getRatio());
			databeans.add(databean);
		}
		return coverMessage("200", "获取成功", databeans);
	}
	/**
	 * 查询血拼数据
	 * @param filter
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/queryShopping")
	public Message queryShopping(@RequestBody IndicatorBreakdownFilter filter){
		List<String> industryNames = new ArrayList<String>();
		industryNames.add("地区生产总值");
		industryNames.add("第一产业增加值");
		industryNames.add("第二产业增加值");
		industryNames.add("第三产业增加值");
		
		filter.setIndustryNames(industryNames);
		//filter = AreaNameUtils.getAreaName(filter);
		List<Map<String, Object>> maps = schedulingService.queryShopping(filter);
		return coverMessage("200", "数据获取成功",maps);
	}
	
	/**
	 * 获取血拼排名列表
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/queryAreaNumberLis")
	public Message queryAreaNumberList(){
		 List<AreaNumber> anList=areaNumberService.query();
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
	     for(AreaNumber am:anList){
	    	 if(!"YT".equals(am.getNumber())){
	    		 Map<String, Object> map2 = new HashMap<String, Object>();
	    		 map2.put("area", am.getName());
	    		 map2.put("number", am.getNumber()+am.getRanking());
	    		 list.add(map2);
	    	 }
	     }
	   return coverMessage("200", "获取成功", list);
	}
	
}
